package com.xry.result.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.TicketResult;
import com.xry.common.bean.TicketResult;
import com.xry.common.service.TicketResultService;
import com.xry.common.service.TicketResultService;
import com.xry.result.mapper.TicketResultMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86152
 * @description 针对表【ticketResult】的数据库操作Service实现
 * @createDate 2025-09-04 19:58:40
 */
@Service
@DubboService
public class TicketResultServiceImpl extends ServiceImpl<TicketResultMapper, TicketResult>
        implements TicketResultService {
    @Override
    public TicketResult getTicketResultById(Integer id) {
        LambdaQueryWrapper<TicketResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TicketResult::getId, id).eq(TicketResult::getDeleted, 0);
        return super.getOne(queryWrapper);
    }


    @Override
    public boolean updateTicketResultById(TicketResult ticketResult) {
        return super.updateById(ticketResult);
    }

    @Override
    public boolean deleteTicketResultById(Integer id) {
        TicketResult ticketResult = super.getById(id);
        if (ticketResult != null){
            ticketResult.setDeleted(1);
            return super.updateById( ticketResult);
        }
        return false;
    }

    @Override
    public boolean addTicketResult(TicketResult ticketResult) {
        return super.save(ticketResult);
    }

    @Override
    public List<TicketResult> getAll() {
        return super.list();
    }

    @Override
    public IPage<TicketResult> getPage(TicketResult ticketResult, IPage<TicketResult> page) {
        LambdaQueryWrapper<TicketResult> queryWrapper = new LambdaQueryWrapper<>();

        if (ticketResult != null) {
            // 1. 修复：先判断userid是否为null，再判断字符串是否有有效文本
            if (ticketResult.getUserid() != null) {
                String useridStr = String.valueOf(ticketResult.getUserid());
                if (StringUtils.hasText(useridStr)) {
                    queryWrapper.like(TicketResult::getUserid, ticketResult.getUserid());
                }
            }

            // 2. 同理修复seatid、matchid、time、result
            if (ticketResult.getSeatid() != null) {
                String seatidStr = String.valueOf(ticketResult.getSeatid());
                if (StringUtils.hasText(seatidStr)) {
                    queryWrapper.like(TicketResult::getSeatid, ticketResult.getSeatid());
                }
            }

            if (ticketResult.getMatchid() != null) {
                String matchidStr = String.valueOf(ticketResult.getMatchid());
                if (StringUtils.hasText(matchidStr)) {
                    queryWrapper.like(TicketResult::getMatchid, ticketResult.getMatchid());
                }
            }

            // 注意：time若为日期类型（如LocalDateTime），不建议用LIKE，建议用范围查询（如between）
            if (ticketResult.getTime() != null) {
                // 若time是字符串类型，按上述逻辑；若为日期类型，需调整查询方式（见优化建议3）
                String timeStr = String.valueOf(ticketResult.getTime());
                if (StringUtils.hasText(timeStr)) {
                    queryWrapper.like(TicketResult::getTime, ticketResult.getTime());
                }
            }

            if (ticketResult.getResult() != null) {
                String resultStr = String.valueOf(ticketResult.getResult());
                if (StringUtils.hasText(resultStr)) {
                    queryWrapper.like(TicketResult::getResult, ticketResult.getResult());
                }
            }
        }

        // 只查询未删除的记录
        queryWrapper.eq(TicketResult::getDeleted, 0);
        // 按时间倒序（建议用创建时间字段，如createTime，避免time字段语义混淆）
        queryWrapper.orderByDesc(TicketResult::getTime);

        return super.page(page, queryWrapper);
    }
}


